/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tablasSql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import otros.MiTableModel;

/**
 *
 * @author FERRETERIA
 */
public class DineroContado {
    private static String tabla = "dinero";
       
   
    public static int guardar (String [] datos, boolean guardar) {
        int numString = 1;
        int numFloat = 4;
        String sql = "";   
        if (!guardar) {
           sql = "update " + tabla + " set fecha = ?, importe = ?, descuento = ?, negro = ? where id = ?";
        }
        else {
            sql = "insert into " + tabla + " (fecha, importe, descuento, negro) ";
            sql += "values (?,?,?,?)";
        }
        return new Sql().actualizarDatos(sql, datos, numString, numFloat);
    }
    
    public static void borrar(String id) {
        String sql = "delete from " + tabla + " where id = " + id;
        new Sql().hacerConsulta(sql);
    }
    
    public static float getDescuento(String id) {
        float valor = 0;
        try {
            String sql = "select descuento from " + tabla + " where id = " + id;
            ResultSet res = new Sql().getDatos(sql);
            if (res.next())
               valor = res.getFloat("descuento");
        } catch (SQLException ex) {
            ;
        }
        return valor;
    }
    
    //--------------------------------------------------------------------------tabla
    
    public static MiTableModel getTaba(int dia, int mes, int año) {
        
        String[] columNames = {"Id","Fecha","Importe", "Descuento","Dinero"}; 
        String[] tableNames = {"id","fecha","importe","descuento", "negro"}; 
        Class [] tipos = {Integer.class, String.class, Float.class, Float.class, Integer.class}; 
        
        String sql = "SELECT * from " + tabla;
        String contar = tabla;
        
        Vector<String> ops = new Vector<String>();
        if (dia != 0)
            ops.add("DAY(" + tabla + ".fecha) = " + dia);
        if (mes != 0)
            ops.add("MONTH(" + tabla + ".fecha) = " + mes);
        if (año != 0)
            ops.add("YEAR(" + tabla + ".fecha) = " + año);
            
        for(int i = 0; i < ops.size(); i++) {
            if (i > 0) {
               sql += " and " + ops.get(i); 
               contar += " and " + ops.get(i);
            }
            else {
                sql += " where " + ops.get(i);
                contar += " where " + ops.get(i);
            }
        }
        sql += " order by " + tabla + ".id desc";
        
        Object[][] dtDatos = new Sql().GetTabla(tableNames, contar, sql, tipos);
        //MiDefaultTableModel dtable = new MiDefaultTableModel(dtDatos,columNames);
        MiTableModel dtable = new MiTableModel(dtDatos,columNames);
        return dtable;    
    }    

}
